package hku.cs.lunuo.rank;

import java.util.ArrayList;
import java.util.List;

public class Des implements RankAlgorithm {
	
	@Override
	public List<Result> compute(Statistic s) {
		PredStatistic statistic = (PredStatistic) s;
		List<Result> result = new ArrayList<Result>();
		int numP = statistic.getTotalFls();
		for (int i = 0, j = statistic.getSize(); i < j; i++) {
			HelpPredStatistic hps = statistic.getSOfClass(i);

			String klassName = hps.getKlassName();

			for (String methodName : h ps.getMethods()) {
				ComPredStatistic cps = hps.getCPSOfMethod(methodName);
				for (String eS : cps.getES()) {
					int fesP = cps.getFlsTimesOfES(eS);
					int sesP = cps.getTruTimesOfES(eS);
					
					
					int tmpblk = Integer.parseInt(eS.split(">")[0]);
					int fPO = cps.getFlsTimesOfPO(tmpblk);
					int sPO = cps.getTruTimesOfPO(tmpblk);
//					System.out.println("~~~~");
					
//					System.out.print("es"+eS+":"+fesP+":"+sesP+"\t");
//					System.out.println("blk"+tmpblk+":"+fPO+":"+sPO);
					
					float failureP = (float)fesP / (fesP + sesP);
					float contextP = (float)fPO / (fPO + sPO);
					float increaseP = failureP - contextP;
//					if(isValide(increaseP)) {
//						System.out.println("increase is valide");
						
						double importantP;
						if(fesP ==1)
							importantP = 0;
						else
							importantP = 2 / (1 / increaseP + Math.log(numP)
							/ Math.log(fesP));

						result.add(new PredResult((float) importantP, klassName, methodName, eS));
				
//					}else
//						System.out.println("increase is not valide");
				}
			}

		}
		return result;
	}

	
	public boolean isValide(float p) {
		if(p<0.000001)
			return false;
		else
			return true;
	}


	@Override
	public boolean isPredRankAlg() {
		return true;
	}


	@Override
	public boolean isStmtRankAlg() {
		return false;
	}


	@Override
	public String getName() {
		return RankAlgorithm.DES;
	}
	
	
}
